interface IAuthType {
  token: string,
  refresh_token: string
}

const TOKEN_KEY = 'geek-h5-token';

/**
 * 获取token
 * @returns 
 */
export const getAuth = (): IAuthType => {
  return JSON.parse(localStorage.getItem(TOKEN_KEY) || "{}")
};

/**
 * 将 Token 信息存入缓存
 * @param {Object} tokenInfo 从后端获取到的 Token 信息
 */
export const saveAuth = (tokenInfo: IAuthType): void => {
  localStorage.setItem(TOKEN_KEY, JSON.stringify(tokenInfo));
};

/**
 * 删除本地缓存中的 Token 信息
 */
export const removeAuth = (): void => {
  localStorage.removeItem(TOKEN_KEY);
};

/**
 * 判断本地缓存中是否存在 Token 信息
 */
export const hasToken = (): boolean => {
  return Boolean(getAuth().token);
};